<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Map' );

global $dal;
$statement = $dal->prepare( 'SELECT faction_id ' .
		'FROM faction_member ' .
		'WHERE ruler_id = :ruler_id AND invited = 0 AND requested = 0' );
$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
$factionId = $dal->getValue( $statement );

$query = "UPDATE ruler " .
		"SET under_attack = 0 " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
mysql_query( $query ) or die( "Query failed : " . mysql_error() );

$query = "SELECT turn, turns_inactive FROM game";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$current_turn = $row['turn'];
$turns_to_inactive = $row['turns_inactive'];

$faction_color_style = '';
$query = "SELECT color, faction_id " .
		"FROM faction_colors " .
		"WHERE faction_id != -1";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$num = mysql_numrows($result);
$i = 0;
while ($i < $num) {
	$faction_color_style .= "#maptable div.faction-color-" . mysql_result($result, $i, 'faction_id') . ", .map-legend .faction-color-" . mysql_result($result, $i, 'faction_id') . " {\n" .
	"	background-color: " . mysql_result($result, $i, 'color') . ";\n" .
	"}\n\n";
	$i++;
}

$query = "SELECT MAX(x), MAX(y) FROM village";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);

$width = $row['MAX(x)'];
$height = $row['MAX(y)'];
//print( "width: " . $width . ", height: " . $height . "<br />" );
$map = Array ();

$query = "SELECT x, y FROM village WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND start_place = 1 LIMIT 1";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);

$display = 40;

if (isset ($_GET['x'])) {
	$start_x = formatInput($_GET['x']);
}
elseif (isset ($row['x'])) {
	$start_x = $row['x'] - $display / 2;
} else {
	$start_x = 0;
}

$centerX = $start_x +20;

if ($start_x < 0) {
	$start_x = 0;
}
elseif ($start_x > $width - $display) {
	if ($width > $display) {
		$start_x = $width - $display;
	} else {
		$start_x = 0;
	}
}

$max_x = $start_x + $display;

if (isset ($_GET['y'])) {
	//print( "Y has been set to: " . $_GET[ 'y' ] . "<br />" );
	$start_y = formatInput($_GET['y']);
}
elseif (isset ($row['y'])) {
	//print( "Y has row is: " . $row[ 'y' ] . "<br />" );
	$start_y = $row['y'] - $display / 2;
} else {
	//print( "Y is: 0<br />" );
	$start_y = 0;
}

$centerY = $start_y +20;
//print( "start_y = $start_y<br />" );

if ($start_y < 0) {
	$start_y = 0;
}
elseif ($start_y > $height - $display) {
	//print( "( start_y = $start_y ) > ( height = $height ) - ( display = $display )<br />" );
	if ($height > $display) {
		$start_y = $height - $display;
	} else {
		$start_y = 0;
	}
}

$max_y = $start_y + $display;

$query = "SELECT width, height FROM map_settings LIMIT 1";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$sectorWidth = $row['width'];
$sectorHeight = $row['height'];
//print( "start_y: " . $start_y . ", max_y: " . $max_y . "<br />" );
?>
					<style type="text/css"><?php print( $faction_color_style ); ?></style> 
					<div class="blok">
<?php
print_operations_tab('Map');
?>
					</div>
					<br />
					<div class="navigation-container">
				    	<table id="navigation">
				    		<th colspan="3"><?php transprint( 'Compass' ); ?></th>
							<tr>
								<td class="even"><a href="javascript:void(null)" onclick="AOS.map.moveMapInDirection( 'north-west' )"><small><?php transprint( 'North west' ); ?></small></a></td>
								<td align="center" class="odd"><a href="javascript:void(null)" onclick="AOS.map.moveMapInDirection( 'north' )"><?php transprint( 'North' ); ?></a></td>
								<td align="right" class="even"><a href="javascript:void(null)" onclick="AOS.map.moveMapInDirection( 'north-east' )"><small><?php transprint( 'North east' ); ?></small></a></td>
							</tr>
							<tr>
								<td class="odd"><a href="javascript:void(null)" onclick="AOS.map.moveMapInDirection( 'west' )"><?php transprint( 'West' ); ?></a></td>
								<td class="even">&nbsp;<br />&nbsp;</td>
								<td align="right" class="odd"><a href="javascript:void(null)" onclick="AOS.map.moveMapInDirection( 'east' )"><?php transprint( 'East' ); ?></a></td>
							</tr>
							<tr>
								<td class="even"><a href="javascript:void(null)" onclick="AOS.map.moveMapInDirection( 'south-west' )"><small><?php transprint( 'South west' ); ?></small></a></td>
								<td align="center" class="odd"><a href="javascript:void(null)" onclick="AOS.map.moveMapInDirection( 'south' )"><?php transprint( 'South' ); ?></a></td>
								<td class="even" align="right"><a href="javascript:void(null)" onclick="AOS.map.moveMapInDirection( 'south-east' )"><small><?php transprint( 'South east' ); ?></small></a></td>
							</tr>
						</table>
					</div>
					<div id="locations-container" class="blok">
						<strong><a href="edit_locations.php"><?php transprint( 'Locations' ); ?></a></strong><br />
						<div id="locations-list">
<?php
print_locations_list();
?>						
						</div>
					</div>
					<div id="mapinfo">
<?php
print (translate("Name") . ": <br />\r\n" .
translate("Location") . ": <br />\r\n" .
translate("Population") . ": <br />\r\n" .
translate("Owner") . ": <br />\r\n" .
translate("Food quality") . ": <br />\r\n" .
translate("Iron quality") . ": <br />\r\n" .
translate("Coal quality") . ": <br />\r\n" .
translate("Free space") . ": <br />\r\n" .
translate("Armies") . ": <br />\r\n");
?>
					</div>
					<br />
					<div id="map_coords">
						<?php transprint("Initializing map..."); ?>
					</div>
					<table class="map-legend floatright">
						<th colspan="2"><?php transprint( 'Map Legend' ); ?></th>
						<tr>
							<td colspan="2">
								<select id="visibility-select" onchange="AOS.map.onVisibilityChange();">
									<option value="none"><?php transprint( 'Show no intelligence' ); ?></option>
									<option value="ruler"><?php transprint( 'Show my intelligence' ); ?></option>
<?php
if( isset( $factionId ) ) {
?>
									<option value="faction"><?php transprint( 'Show faction intelligence' ); ?></option>
<?php	
}
?>
								</select>
							</td>
						</tr>
						<tr><td align="right"><div class="o">&nbsp;</div></td><td><?php transprint( 'Own village' ); ?></td></tr>
						<tr><td align="right"><div class="a">&nbsp;</div></td><td><?php transprint( 'Allied village' ); ?></td></tr>
						<tr><td align="right"><div class="h">&nbsp;</div></td><td><?php transprint( 'Hostile village' ); ?></td></tr>
						<tr><td align="right"><div class="i">&nbsp;</div></td><td><?php transprint( 'Independent village' ); ?></td></tr>
						<tr><td align="right"><div class="oc">&nbsp;</div></td><td><?php transprint( 'Own capital' ); ?></td></tr>
						<tr><td align="right"><div class="ac">&nbsp;</div></td><td><?php transprint( 'Allied capital' ); ?></td></tr>
						<tr><td align="right"><div class="hc">&nbsp;</div></td><td><?php transprint( 'Hostile capital' ); ?></td></tr>
						<tr><td align="right"><div class="h inactive"><img src="../resources/inactive.gif" /></div></td><td><?php transprint( 'Inactive hostile village' ); ?></td></tr>
						<tr><td align="right"><div class="own">&nbsp;</div></td><td><?php transprint( 'Own stationary army' ); ?></td></tr>
						<tr><td align="right"><div class="own moving">&nbsp;</div></td><td><?php transprint( 'Own moving army' ); ?></td></tr>
						<tr><td align="right"><div class="own bombard">&nbsp;</div></td><td><?php transprint( 'Own bombarding army' ); ?></td></tr>
						<tr><td align="right"><div class="own fortified">&nbsp;</div></td><td><?php transprint( 'Own fortified army' ); ?></td></tr>
<?php
$query = "SELECT faction_id " .
		"FROM faction_member " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( isset( $row[ 'faction_id' ] ) ) {
	$faction_id = $row[ 'faction_id' ];
}
else {
	$faction_id = -1;
}

$query = "SELECT color, faction.faction_id, faction.tag, faction.name " .
		"FROM faction_colors " .
		"JOIN faction ON faction.faction_id = faction_colors.faction_id " .
		"WHERE faction_colors.faction_id != -1 " .
		"ORDER BY faction.name";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$num = mysql_numrows($result);
$i = 0;
while ($i < $num) {
	if( mysql_result($result, $i, 'faction_id') != $faction_id ) {
?>
						<tr><td align="right"><div class="h faction-color-<?php print( mysql_result($result, $i, 'faction_id' ) )?>">&nbsp;</div></td><td><?php transprint( 'Village from' ); ?> <a href="../overview/faction.php?name=<?php print( mysql_result($result, $i, 'name' ) ); ?>">[<?php print( strtoupper( mysql_result($result, $i, 'tag' ) ) ); ?>]</a></td></tr>
<?php
	}
	$i++;
}

?>							
					</table>
					<div id="another-map-container">
						<div id="map_overlay">
							<div id="maptable">
								&nbsp;
							</div>
						</div>
					</div>
<?php
$max_locations = get_max_locations();
?>
					<div class="hidden" id="max-locations"><?php print( $max_locations ); ?></div>
					<small>(<?php transprint( 'Hold down the left mouse button over the map and drag your mouse to move the map' ); ?>)</small>
<?php
$added_code = "<script type=\"text/javascript\" src=\"../global/yui/dragdrop/dragdrop-min.js\"></script>\n" .
		"<script type=\"text/javascript\" src=\"map.js\"></script>\n" .
		"<script language=\"JavaScript\" type=\"text/javascript\">\n" .
		"AOS.map.width = $width;\n" . 
		"AOS.map.height = $height;\n" . 
		"AOS.map.start_x = $start_x;\n" .
		"AOS.map.start_y = $start_y;\n" .
		"AOS.map.centerX = $centerX;\n" .
		"AOS.map.centerY = $centerY;\n" .
		"AOS.map.display = $display;\n" .
		"AOS.map.sectorWidth = $sectorWidth;\n" .
		"AOS.map.sectorHeight = $sectorHeight;\n" .
		"AOS.map.storeLocationText = \"" . translate( 'Store location' ) . "\";\n" .
		"YAHOO.util.Event.addListener( window, \"load\", AOS.map.initialize );\n" .
		"</script>\n" . 
		"<script type=\"text/javascript\" src=\"../global/global.js\"></script>\n";

aos_footer( $added_code );
?>